package proxy_cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName ProxyFactory
 * @Date 2021/7/27 12:03
 * @Author 李明杰
 * @Description TODO
 */

public class ProxyFactory implements MethodInterceptor {
    //要代理的原始对象
    private Object object;

    //1创建代理对象
    public Object createProxy(Object target) {
        this.object = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.object.getClass());
        enhancer.setCallback(this);
        Object o = enhancer.create();
        return o;
    }

    //2.实现方法
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        return methodProxy.invokeSuper(o, objects);
    }
}
